/**
 * @file 工单操作-算账
 * @author yangjiangming <449654337@qq.com>
 * @date 2018-12-10 20:13:16
 */
layui.define(['layer', 'form'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var opCommonUtils = layui.list_modify_common;


    /**
     * 初始化页面
     */
    function initPage() {
        eachRadio();
        $('#coupon').hide();
    }

    /**
     * 初始化页面事件
     */
    function initEvent() {
        // 查看大图
        layer.photos({
            photos: '#form-platreview'
            , anim: 5
        });

        // 改变审核状态
        changeRadio();

        // 保存
        form.on("submit(save)",function (obj){

            var data = obj.field;
            /*if(eachRadio()){
                popupConfirm(data);//弹出确认框
                return false;
            }*/
            if(eachRadio()){
                popupConfirmFailed(data);//失败弹出确认框
            }else{
                popupConfirmSuccess(data);//成功弹出确认框
            }
            /*request({
                data:JSON.stringify(data),
                success:function (res){
                    if(res.status == 200){
                        toast({
                            title:'操作成功',
                            icon:1
                        })
                        return false;
                    }
                    toast({
                        title:'操作失败',
                        icon:2,
                        time:2000
                    })
                    return false;
                }
            })*/
            return false;
        });

        // 重置
        $(".reset-btn").on("click", function() {
            $("#form-platreview").reset();
            $(".platreview-fail-remark").hide();
            form.render();
        });

        // 监听查看地图按钮点击事件
        $('#btn-map').on('click', function () {
            if (!workPoint || !workPoint.userPoint || !workPoint.masterPoint) {
                layer.msg('用户或工程师定位信息不全', { icon: 2 });
                return;
            }

            layer.open({
                type: 1,
                title: '地图：A-工程师位置 B-客户位置',
                skin: 'layui-layer-rim',
                area: ['80%', '80%'],
                content: '<div id="map-container" style="height: 100%"></div>',
                success: function () {
                    var map = new BMap.Map("map-container");

                    // 创建点
                    var userPoint = new BMap.Point(workPoint.userPoint.lng, workPoint.userPoint.lat);
                    var masterPoint = new BMap.Point(workPoint.masterPoint.lng, workPoint.masterPoint.lat);

                    map.centerAndZoom(userPoint, 15);

                    // 启用工具
                    map.enableScrollWheelZoom(true);
                    map.addControl(new BMap.NavigationControl());
                    map.addControl(new BMap.ScaleControl());

                    var userIcon = new BMap.Icon("/static/images/map-markers.png", new BMap.Size(23, 25), {
                        offset: new BMap.Size(10, 25),
                        imageOffset: new BMap.Size(0, 0 - 1 * 25)
                    });

                    var masterIcon = new BMap.Icon("/static/images/map-markers.png", new BMap.Size(23, 25), {
                        offset: new BMap.Size(10, 25),
                        imageOffset: new BMap.Size(0, 0 - 0 * 25)
                    });

                    // 添加标注
                    var userMarker = new BMap.Marker(userPoint, { icon: userIcon });
                    var masterMarker = new BMap.Marker(masterPoint, { icon: masterIcon });
                    map.addOverlay(userMarker);
                    map.addOverlay(masterMarker);

                    // 添加连线
                    var polyline = new BMap.Polyline([userPoint, masterPoint],
                        { strokeColor:"blue", strokeWeight:3, strokeOpacity:0.5 }
                    );
                    map.addOverlay(polyline);
                }
            });
        });
    }

    // 程序入口
    function init() {

        // 初始化页面
        initPage();

        // 初始化事件
        initEvent();

    }


    // 初始化
    init();

    /**
     * 遍历所有的单选按钮状态,并显示审核失败的备注框
     */
    function eachRadio() {
        var inputRadioList = $("input[type=radio]");
        var isHaveFail = false;
        $.each(inputRadioList, function (index,item) {
            var isChecked = $(this).prop("checked");
            var val = $(this).val();
            if( isChecked && val == 3 ){
                //审核未通过
                isHaveFail = showFail(item);
            }else{
                //审核通过
                hideFail(item)
            }
        })
        return isHaveFail;
    }

    /**
     *隐藏审核失败的备注框
     * @param boo {boolean} 选中状态
     * @param ele
     */
    function hideFail(ele){
        var platreviewFailRemark = $(ele).parent().siblings(".platreview-fail-remark");
        var textarea = $(ele).parent().siblings(".platreview-fail-remark").find("textarea");
        textarea.val("");
        textarea.removeAttr("lay-verify");
        platreviewFailRemark.hide();
        return false;
    }

    /**
     * 显示审核失败备注框
     * @param ele 选中审核失败的当前radio
     * @returns {boolean}
     */
    function showFail(ele){
        var platreviewFailRemark = $(ele).parent().siblings(".platreview-fail-remark");
        var textarea = $(ele).parent().siblings(".platreview-fail-remark").find("textarea");
        platreviewFailRemark.show();
        textarea.attr("lay-verify","required");
        return true;
    }

    /**
     * 改变审核状态
     */
    function changeRadio(){
        form.on('radio()', function(res) {
            var val = res.value;
            var platreviewFailRemark = $(this).parent('div').siblings('.platreview-fail-remark');
            var textarea = $(this).parent().siblings(".platreview-fail-remark").find("textarea");
            if (val == 3) {
                textarea.attr("lay-verify","required");
                platreviewFailRemark.show();
            } else {
                textarea .val("");
                textarea.removeAttr("lay-verify");
                platreviewFailRemark.hide();
            }

            var inputRadioList = $("input[type=radio]");
            var inputRadiochecked = $("input[type=radio][value='2']:checked");
            if (inputRadiochecked != null && inputRadiochecked.length == inputRadioList.length/2) {
                /*var isHaveSuccess = true;
                $.each(inputRadioList, function (index,item) {
                    var isChecked = $(this).prop("checked");
                    var val = $(this).val();
                    if( isChecked && val == 3 ){
                        //审核通过
                        isHaveSuccess = false;
                    }
                })
                if (isHaveSuccess) {
                    $('#coupon').show();
                } else {
                    $('#coupon').hide();
                }*/
                $('#coupon').show();
            } else {
                $('#coupon').hide();
            }
        })
    }

    /**
     * ajax 请求
     * @param data {object} data,success,fail属性
     */
    function request(data) {
        $.ajax({
            url: '../save/plat/review.action',
            type: 'post',
            contentType: 'application/json;charset=UTF-8',
            dataType: "json",
            data: data.data,
            success: function (res) {
                if (typeof data.success == "function") {
                    data.success(res);
                }
            },
            fail: function (err) {
                if (typeof data.fail == "function") {
                    data.fail(err);
                }
            }
        })
    }

    /**
     * 提示框
     * @param str {string} 提示内容;
     * @returns {*}
     */
    function toast(opt) {
        layer.msg(opt.title,{
            icon:opt.icon,
            time:opt.time || 1000,
        },function () {
            if(opt.icon == 1){
                opCommonUtils.goOperateHome();
            }
        })
    }


    /**
     * 弹出确认框
     */
    function popupConfirm(e){
        layer.open({
            type: 1
            ,title: "提示"
            ,area: '300px;'
            ,shade: 0.8
            ,id: 'LAY_layuipro'
            ,btn: ['确认', '取消']
            ,btnAlign: 'r'
            ,moveType: 1
            ,content: '<div style="padding: 20px;color: #000; line-height: 22px; font-weight: 300;">确定审核失败</div>'
            ,success: function(layero){
                var btn = layero.find('.layui-layer-btn');
                btn.on("click",".layui-layer-btn0",function (){
                    request({
                        data:JSON.stringify(e),
                        success:function (res){
                            if(res.status == 200){
                                toast({
                                    title:'操作成功',
                                    icon:1
                                })
                                return false;
                            }
                            toast({
                                title:'操作失败',
                                icon:2,
                                time:2000
                            })
                            return false;
                        }
                    })

                })
            },
        });
    }

    /**
     * 审核成功弹出确认框
     */
    function popupConfirmSuccess(e){
        layer.open({
            type: 1
            ,title: "提示"
            ,area: '300px;'
            ,shade: 0
            ,id: 'LAY_layuipro'
            ,btn: ['确认', '取消']
            ,btnAlign: 'r'
            ,skin: 'layui-layer-molv'
            ,moveType: 1
            ,content: '<div style="padding: 20px;color: #000; line-height: 22px; font-weight: 300;">确定审核成功？审核成功后，订单金额固定，将不再可更改</div>'
            ,success: function(layero){
                var btn = layero.find('.layui-layer-btn');
                btn.on("click",".layui-layer-btn0",function (){
                    request({
                        data:JSON.stringify(e),
                        success:function (res){
                            if(res.status == 200){
                                toast({
                                    title:'操作成功',
                                    icon:1
                                })
                                return false;
                            }
                            toast({
                                title:'操作失败',
                                icon:2,
                                time:2000
                            })
                            return false;
                        }
                    })

                })
            },
        });
    }

    /**
     * 审核失败弹出确认框
     */
    function popupConfirmFailed(e){
        layer.open({
            type: 1
            ,title: "提示"
            ,area: '300px;'
            ,shade: 0
            ,id: 'LAY_layuipro'
            ,btn: ['确认', '取消']
            ,btnAlign: 'r'
            ,skin: '.layui-layer-molv'
            ,moveType: 1
            ,content: '<div style="padding: 20px;color: #000; line-height: 22px; font-weight: 300;">确定审核失败？审核失败为不可逆操作</div>'
            ,success: function(layero){
                var btn = layero.find('.layui-layer-btn');
                btn.on("click",".layui-layer-btn0",function (){
                    request({
                        data:JSON.stringify(e),
                        success:function (res){
                            if(res.status == 200){
                                toast({
                                    title:'操作成功',
                                    icon:1
                                })
                                return false;
                            }
                            toast({
                                title:'操作失败',
                                icon:2,
                                time:2000
                            })
                            return false;
                        }
                    })

                })
            },
        });
    }


    exports('oms_member_work_modify_platreview');
});
